#!/usr/bin/env node

const axios = require('axios'),
      cheerio = require('cheerio'),
      path = require('path'),
      fs = require('fs'),
      baseURL = 'http://www.dm5.com/';

      async function getBookData(){
        var files = fs.readdirSync('./data')
        var fileData, file, arrID, url, res, tip, i=1, j

        files.forEach(async(file) => {
          file = path.join(__dirname, 'data', file)
          console.log(`file:${file}`)

          fileData = fs.readFileSync(file).toString('utf8')
          fileData = JSON.parse(fileData)
          fileData = fileData.UpdateComicItems

          fileData.forEach(async(book) => {
            url = baseURL + book.UrlKey + '/'
            res = await axios.get(url)
            res = res.data

            var $ = cheerio.load(res)
            tip = $('body').find('.tip>.block')
            summary = $('body').find('.info>.content')

            console.log(`${i++} ${book.Title.substr(0, 10)}\t${url}`)
            console.log($(tip[0]).text())
            console.log($(tip[1]).text().split(' ').reduce((tag, item) => {
              return tag +item;
            },''))

          console.log('简介：' + summary.text().substr(0, 30))
            console.log('')
          })
        })
      }

getBookData();
